﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using SsTool.Core.Windows;
using SmartSpace.Core;
using SsTool.Common;
using SsTool.Core.SubPages;

namespace SsTool.Core
{
    /// <summary>
    /// Interaction logic for ApplicationListPage.xaml
    /// </summary>
    public partial class ApplicationListPage : Page
    {
        public ApplicationListPage()
        {
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(ApplicationListPage_Loaded);
        }

        void ApplicationListPage_Loaded(object sender, RoutedEventArgs e)
        {
            LoadData();
        }

        private void LoadData()
        {
            ICoreService service = App.Get<ICoreService>();
            _ctlGrid.ItemsSource = service.GetApplications();
        }

        private void _ctlAdd_Click(object sender, RoutedEventArgs e)
        {
            ApplicationWindow window = new ApplicationWindow();
            if (window.ShowDialog() == true)
            {
                LoadData();
            }
        }

        private void _ctlEdit_Click(object sender, RoutedEventArgs e)
        {
            SmartSpace.Core.Application selectedItem = _ctlGrid.SelectedItem as SmartSpace.Core.Application;
            if (selectedItem != null)
            {
                ApplicationWindow window = new ApplicationWindow(selectedItem);
                if (window.ShowDialog() == true)
                {
                    LoadData();
                }
            }
        }

        private void _ctlDelete_Click(object sender, RoutedEventArgs e)
        {

        }

        private void _ctlGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            SmartSpace.Core.Application selectedItem = _ctlGrid.SelectedItem as SmartSpace.Core.Application;
            if (selectedItem != null)
            {
                _ctlNavigationPart.SetApplication(selectedItem);
                _ctlShortcutsPart.SetApplication(selectedItem);
                ICoreService service = App.Get<ICoreService>();
                _ctlGridModules.ItemsSource = service.GetModules(selectedItem.Id);
            }
        }

        private void _ctlBtnAddModule_Click(object sender, RoutedEventArgs e)
        {
            SmartSpace.Core.Application selectedItem = _ctlGrid.SelectedItem as SmartSpace.Core.Application;
            if (selectedItem != null)
            {
                SelectModuleWindow window = new SelectModuleWindow();
                if (window.ShowDialog() == true)
                {
                    Module selectedModule = window.SelectedModule;

                    ICoreService service = App.Get<ICoreService>();
                    if (service.AddModule(selectedItem.Id, selectedModule.Id))
                    {
                        MessageBox.Show("添加成功!");
                        _ctlGridModules.ItemsSource = service.GetModules(selectedItem.Id);
                    }
                    else
                    {
                        MessageBox.Show("添加失败!");
                    }
                }
            }
        }

        private void _ctlBtnRemoveModule_Click(object sender, RoutedEventArgs e)
        {
            SmartSpace.Core.Application selectedItem = _ctlGrid.SelectedItem as SmartSpace.Core.Application;

            Module selectedModule = _ctlGridModules.SelectedItem as SmartSpace.Core.Module;
            if (selectedItem != null && selectedModule != null)
            {
                ICoreService service = App.Get<ICoreService>();
                if (service.RemoveModule(selectedItem.Id, selectedModule.Id))
                {
                    MessageBox.Show("删除成功!");
                    _ctlGridModules.ItemsSource = service.GetModules(selectedItem.Id);
                }
                else
                {
                    MessageBox.Show("删除失败!");
                }
            }
        }

        private void DataGridHyperlinkColumn_Click(object sender, RoutedEventArgs e)
        {
            SmartSpace.Core.Application selectedItem = (sender as Button).DataContext as SmartSpace.Core.Application;
            NavigationService.Navigate(new RoleNavigationPage(selectedItem));
        }
    }
}
